﻿using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Xml.XPath;
using System.Xml;
using umbraco;
using umbraco.DataLayer;

namespace UComment.Library
{
    [XsltExtension("UCommentLibrary")]
    public class XsltLibrary
    {
        [ObsoleteAttribute("getGravatar is obsolete please use GetGravatar")]
        public static string getGravatar(string email, int size, string defaultImage)
        {
            return GetGravatar(email, size, defaultImage);
        }

        public static string GetGravatar(string email, int size, string defaultImage)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            var result = md5.ComputeHash(Encoding.ASCII.GetBytes(email));

            var hash = new StringBuilder();
            for (var i = 0; i < result.Length; i++)
                hash.Append(result[i].ToString("x2"));

            var image = new StringBuilder();

            image.Append("http://www.gravatar.com/avatar.php?");
            image.Append("gravatar_id=" + hash);
            image.Append("&amp;rating=G");
            image.Append("&amp;size=" + size);
            image.Append("&amp;default=");
            image.Append(HttpContext.Current.Server.UrlEncode(defaultImage));

            return image.ToString();
        }

        public static string Dictionary(string text, string fallback)
        {
            var key = text.Trim().TrimStart('#');
            var result = library.GetDictionaryItem(key);

            if (result.Length > 0)
                return result;

            return !string.IsNullOrEmpty(fallback) ? fallback : key;
        }

        public static XPathNodeIterator GetComments()
        {           
            var sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
            var rr = sqlHelper.ExecuteReader("select * from comment where spam != 1");
            return CommentsToXml(rr);
        }

        public static XPathNodeIterator GetCommentsForNode(int nodeid)
        {

            var sqlHelper = DataLayerHelper.CreateSqlHelper(GlobalSettings.DbDSN);
           
            var rr = sqlHelper.ExecuteReader("select * from comment where nodeid = @nodeid and spam != 1",
                 sqlHelper.CreateParameter("@nodeid", nodeid));

            return CommentsToXml(rr);
        }

        private static XPathNodeIterator CommentsToXml(IRecordsReader rr)
        {
            var xd = new XmlDocument();
            var x = xmlHelper.addTextNode(xd, "comments", "");

            while (rr.Read())
            {
                XmlNode c = xd.CreateElement("comment");

                c.Attributes.Append(xmlHelper.addAttribute(xd, "id", rr.GetInt("id").ToString()));
                c.Attributes.Append(xmlHelper.addAttribute(xd, "nodeid", rr.GetInt("nodeid").ToString()));
                c.Attributes.Append(xmlHelper.addAttribute(xd, "created", rr.GetDateTime("created").ToString()));

                c.AppendChild(xmlHelper.addCDataNode(xd, "name", rr.GetString("name")));
                c.AppendChild(xmlHelper.addCDataNode(xd, "email", rr.GetString("email")));
                c.AppendChild(xmlHelper.addCDataNode(xd, "website", rr.GetString("website")));
                c.AppendChild(xmlHelper.addCDataNode(xd, "message", rr.GetString("comment")));

                x.AppendChild(c);
            }

            xd.AppendChild(x);
            return xd.CreateNavigator().Select(".");
        }
    }
}
